import { reqGetGoodsInfo,reqAddOrUpdateCartNum } from '@/api';
import { getUUID } from '@/utils/uuid';

const state = {
    goodInfo: {},
    uuid_token: getUUID()
};

const getters = {
    categoryView(state) {
        return state.goodInfo.categoryView || {};
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {};
    },
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || [];
    },
};

const mutations = {
    GETGOODSINFO(state,goodInfo) {
        state.goodInfo = goodInfo;
    }
};

const actions = {
    // 获取商品详情数据
    async getGoodsInfo({commit},skuId) {
        let res = await reqGetGoodsInfo(skuId);
        if(res.code == 200) {
            commit("GETGOODSINFO",res.data);
        }
    },
    // 加入购物车
    async addOrUpdateCartNum({commit},{skuId,skuNum}) {
        let res = await reqAddOrUpdateCartNum(skuId,skuNum);
        if(res.code == 200) {
            return "ok";
        }
        return Promise.reject(new Error('false'));
    }
};

export default {
    state,
    getters,
    mutations,
    actions
}